//
//  VisitorLoginView.swift
//  SinaWeibo
//
//  Created by apple on 15/11/8.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit

@objc protocol VisitorLoginViewDelegate:NSObjectProtocol {
    
    /**
    *  在代理中声明代理法方法
    */
    optional func visitorLogin()
    
    func visitorRegister()
    
}
class VisitorLoginView: UIView {
    
    //声明属性使用weak
    weak var visitordelegate:VisitorLoginViewDelegate?
    
    //在按钮的点击事件中让调用代理方法
    @objc func loginBtnClick (){
        
        //让代理调用代理方法
        visitordelegate?.visitorLogin?()
        
    }
    @objc func  registerBtnClick(){
        
        //让代理调用代理方法
        visitordelegate?.visitorRegister()
    }
    
    //设置每个控制器访客视图的方法
    func setUPVisitor (imageName:String?, title:String) {
        
        iconView.hidden = false
        if imageName != nil {
            
            circleView.image = UIImage(named:imageName!)
            iconView.hidden = true
            
        } else {
            
            stratAnimation()
        }
        
        tipLabel.text = title
        
    }
    
    //添加动画
    func stratAnimation () {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        anim.duration = 15
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
    }
    //重写构造方法
    init() {
        
        super.init(frame:CGRectZero)
        setUPUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setUPUI () {
        
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        addSubview(tipLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        for v in subviews {
            
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        //设置布局
        //VFL
        //设置 circleView 的布局
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        //设置iconView 的布局
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: -10))
        
        // 设置提示文案的布局
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 30))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50))
        
        
        //设置登录按钮的布局
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        //设置注册按钮
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //设置蒙版的约束
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-35)-[registerBtn]", options: [], metrics: nil, views: ["backView":backView ,"registerBtn": registerBtn]))
        
        //设置背景颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        //添加注册 和 登陆按钮的监听事件
        loginBtn .addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        registerBtn .addTarget(self, action: "registerBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    //懒加载所有控件
    
    //circleView  和 iconView
    private lazy var circleView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var iconView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var backView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //提示文案
    private lazy var tipLabel:UILabel = UILabel(title: "关注一些人 回这里看看有什么惊喜 关注一些人 回这里看看有什么惊喜", size: 14, color:  UIColor.lightGrayColor())
    
    
    //登录按钮
    private var loginBtn : UIButton = UIButton(title: "登陆", backImage: "common_button_white_disable", color: UIColor.lightGrayColor())
    
    //注册按钮
    private var registerBtn : UIButton = UIButton(title: "注册", backImage: "common_button_white_disable", color: UIColor.orangeColor())
    
}
